﻿using System;
using System.Web.Mvc;
using TeamsPortal.DataAccess;
using TeamsPortal.ServiceClient;
using TeamsPortal.Web.Controllers.Helpers;

namespace TeamsPortal.Web.Controllers
{
	public class TopicsSubscriptionsController : BaseController
	{
        private ITopicsServiceClient _topicServiceClient;
        private IUsersServiceClient _usersServiceClient;
        private ITopicsSubscriptionsServiceClient _topicSubscriptionServiceClient;

        public TopicsSubscriptionsController(ITopicsServiceClient topicServiceClient, IUsersServiceClient usersServiceClient, ITopicsSubscriptionsServiceClient topicSubscriptionServiceClient)
        {
            _topicServiceClient = topicServiceClient;
            _usersServiceClient = usersServiceClient;
            _topicSubscriptionServiceClient = topicSubscriptionServiceClient;
        }

		/// <summary>
		/// Unsubscribes a user from a topic
		/// </summary>
		/// <param name="guid">user guid</param>
		/// <param name="id">topic id</param>
		public ActionResult Unsubscribe(int uid, string guid, int tid)
		{
			Guid parsedGuid = Guid.Empty;
			Topic topic = null;
			#region Parse guid
			try
			{
				parsedGuid = new Guid(guid);
			}
			catch
			{
				ResultHelper.ForbiddenResult(this);
			} 
			#endregion
            int removedSubscription = _topicSubscriptionServiceClient.Remove(tid, uid, parsedGuid);

			if (removedSubscription > 0)
			{
				//Load the user data
                ViewData["User"] = _usersServiceClient.Get(uid);
			}
            topic = _topicServiceClient.Get(tid);
			return View(topic);
		}
	}
}
